<?php
namespace app\shop\controller;

use app\common\controller\Shop;
use app\common\model\Business\Business;
use app\common\model\Chat as ModelChat;
use think\Request;

class Chat extends Shop
{
    protected $model = null;
    protected $businessModel = null;
    public function __construct(Request $request)
    {
        $this->model = new ModelChat();
        $this->businessModel = new Business();
        $this->noLogin = ['chat'];
        parent::__construct($request);
    }
    public function chat()
    {
        if($this->request->isPost())
        {
            $this->request->filter('trim,strip_tags');
            $code = $this->request->post('code',0);
            $result = [
                'code'=>0,
                'msg'=>''
            ];
            if($code==1){
                $sendid = $this->request->post('sendid',0);
                $send = $this->businessModel->get($sendid);
                if(!$send)return json_encode($result);
                $receiveid = $this->request->post('receiveid',0);
                $content = $this->request->post('content','');
                $params = [
                    'sendid'=>$sendid,
                    'receiveid'=>$receiveid,
                    'content'=>$content,
                ];
                $res = $this->model->create($params,true);
                if($res===false)return json_encode($result);
                $result = [
                    'code'=>1,
                    'msg'=>'发送成功'
                ];
                return json_encode($result, JSON_UNESCAPED_UNICODE);
            }
        }else if($this->request->isGet())
        {
            $sendid = $this->request->get('sendid',0);
            $receiveid = $this->request->get('receiveid',0);
            $page = $this->request->get('page',1);
            $pageSize = 30;
            $chat = $this->model
            // ->whereOr(['sendid'=>$sendid,'receiveid'=>$receiveid])
            // ->whereOr(['receiveid'=>$sendid,'sendid'=>$receiveid])
            ->where("(sendid=".$sendid." AND receiveid=".$receiveid.") OR (sendid=".$receiveid." AND receiveid=".$sendid.")")
            ->order('createtime','desc')
            ->paginate($pageSize,$page);
            return $chat;
        }
    }
    public function share()
    {
        $busid = $this->business['id'];
        $where = ['sendid'=>$busid,'receiveid'=>['neq',$busid]];
        $receives = $this->model->field('receiveid,max(id) as id')->where($where)->group('receiveid')->select();
        $where = ['receiveid'=>$busid,'sendid'=>['neq',$busid]];
        $sends = $this->model->field('sendid,max(id) as id')->where($where)->group('sendid')->select();
        $idlist = [];
        foreach($receives as $receive)
        {
            $idlist[$receive['receiveid']] = $receive['id'];
        }
        foreach($sends as $send)
        {
            $sendid = $send['sendid'];
            if(isset($idlist[$sendid]))
            {
                if($idlist[$sendid]<$send['id'])$idlist[$sendid] = $send['id'];
            }else{
                $idlist[$sendid] = $send['id'];
            }
        }
        $list = $this->model->where('id','in',array_values($idlist))->select();
        foreach($list as $item)
        {
            $counterpartid = $item['sendid']==$busid?$item['receiveid']:$item['sendid'];
            $item['counterpart'] = $this->businessModel->get($counterpartid);
        }
        return $list;
    }
}
